<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户来源" prop="sourceId">
        <el-select
          v-model="queryParams.sourceId"
          placeholder="请选择客户来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_CUSTOMER_SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属行业" prop="industryId">
        <el-select
          v-model="queryParams.industryId"
          placeholder="请选择所属行业"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_INDUSTRY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户等级" prop="levelId">
        <el-select
          v-model="queryParams.levelId"
          placeholder="请选择客户等级"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_CUSTOMER_LEAVE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户意向" prop="intentId">
        <el-select
          v-model="queryParams.intentId"
          placeholder="请选择客户意向"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_INTENT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['oa:customer:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增跟进
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="客户名称" align="left" prop="customerName" width="200">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_LEAVE" :value="scope.row.levelId" />
          <text>{{ scope.row.customerName }}</text>
        </template>
      </el-table-column>
      <el-table-column label="客户状态" align="center" prop="statusId" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_STATUS" :value="scope.row.statusId" />
        </template>
      </el-table-column>
      <el-table-column label="客户意向" align="center" prop="intentId">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_INTENT" :value="scope.row.intentId" />
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactName" width="100" />
      <el-table-column label="联系电话" align="center" prop="phone" width="120" />
      <el-table-column label="客户来源" align="center" prop="sourceId" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_SOURCE" :value="scope.row.sourceId" />
        </template>
      </el-table-column>
      
      <el-table-column label="所属行业" align="center" prop="industryId" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_INDUSTRY" :value="scope.row.industryId" />
        </template>
      </el-table-column>
      <el-table-column label="客户等级" align="center" prop="levelId">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OA_CUSTOMER_LEAVE" :value="scope.row.levelId" />
        </template>
      </el-table-column>
      
      <el-table-column label="所属员工" align="center" prop="recipientName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row.id)"
            v-hasPermi="['oa:customer:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleEdit(scope.row)"
            v-hasPermi="['oa:customer:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleMoveToPublic(scope.row)"
            v-hasPermi="['oa:customer:update']"
          >
            移入公海
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 详情弹窗 -->
  <CustomerDetail ref="detailRef" />
  <!-- 表单弹窗：添加/修改 -->
  <CustomerForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { CustomerApi, CustomerVO } from '@/api/oa/customer'
import CustomerDetail from './CustomerDetail.vue'
import CustomerForm from './CustomerForm.vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter } from 'vue-router'

/** 客户跟进页面 */
defineOptions({ name: 'CustomerFollow' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const userStore = useUserStore() // 用户store
const router = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const list = ref<CustomerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerName: undefined,
  sourceId: undefined,
  industryId: undefined,
  levelId: undefined,
  intentId: undefined,
  statusId: 1,
})
const queryFormRef = ref() // 搜索的表单
const formRef = ref() // 表单 Ref
const detailRef = ref() // 详情 Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerApi.getCustomerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查看详情操作 */
const handleDetail = (id: number) => {
  detailRef.value?.open(id)
}

/** 处理编辑操作 */
const handleEdit = (row: CustomerVO) => {
  formRef.value?.open('update', row.id)
}

/** 处理移入公海 */
const handleMoveToPublic = async (row: CustomerVO) => {
  try {
    await message.confirm('确认将该客户移入公海吗？')
    await CustomerApi.moveToTrash(row.id, 0) // 0 表示公海状态
    message.success('移入公海成功')
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script> 